<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Error_code_library {
    public static $codeType = array(
        'error' => array('code' => 0X1, 'name' => 'Error:')
    );
    /**
     * 系统编号
     */
    public static $systemCode = array(
        'crawl'   => array('code' => 0X4, 'name' => 'Crawl')
    );

    /**
     * 子系统编号
     */
    public static $subSystemCode = array(
        'keyword'   => array('code' => 0X4, 'name' => 'Content By KeyWord'),
    );

    /**
     * 模块编号
     */
    public static $modulesCode = array(
        'system'        => array('code' => 0x0, 'name' => 'System'),
        'curl'        => array('code' => 0x1, 'name' => 'CURL'),
        'token'        => array('code' => 0x2, 'name' => 'User Token'),
        'make_data'        => array('code' => 0x3, 'name' => 'Data Analyst'),
        'retry'          => array('code' => 0x4, 'name' => 'Retry Crawl'),
        ''       => array('code' => 0x5, 'name' => ''),
        ''       => array('code' => 0x6, 'name' => ''),
        ''   => array('code' => 0x7, 'name' => ''),
        ''    => array('code' => 0x8, 'name' => '')
    );

    /**
     * 错误类型
     */
    public static $errorType = array(
        'common'    => array('code' => 0x0, 'name' => 'Common Errors'),
        'runtime'   => array('code' => 0x1, 'name' => 'RunTime Abnormal'),
        'data'      => array('code' => 0x2, 'name' => 'Data Error'),
        'params'    => array('code' => 0x3, 'name' => 'Params Error')
    );

    /**
     * 错误编码和错误具体信息
     */
    public static $specificError = array(
        //系统相关错误
        'no_auth_parameter'     => array('code' => '00000001', 'name' => 'Can`t Find Auth Info In Parameters'),             //1
        'no_data'               => array('code' => '00000010', 'name' => 'Can`t Find Matching Data'),                                 //2
        'auth_failed'          => array('code' => '00000011', 'name' => 'This Request Use Token Is Invalid'),          //3
        ''         => array('code' => '00000100', 'name' => ''),                             //4
        'no_token'              => array('code' => '00000101', 'name' => 'Can`t Find Usably User Token For '),                              //5
        'no_appkey'             => array('code' => '00010111', 'name' => 'No Match for This Token: '),                       //23
        'illegal'               => array('code' => '00011000', 'name' => 'Illegal Keywords'),                      //24
        ''         => array('code' => '00011111', 'name' => ''), //31
        'failed'                => array('code' => '00100000', 'name' => 'Crawl Data FAILED'), //32
        //抓取相关
        'ip_usability_failed'             => array('code' => '00000110', 'name' => 'Ip Usability Test Failed'),                                       //6
        'curl_post'           => array('code' => '00000111', 'name' => 'CURL Post Failed'),                             //7
        'decode'       => array('code' => '00001000', 'name' => 'Can`t Decode The Response: '),                           //8
        'return_error'         => array('code' => '00001111', 'name' => 'Response An Error With This Request'),                            //15
        'verification_code'         => array('code' => '00010000', 'name' => 'This Request Need Verification Code'),                         //16
        'incomplete_answers'           => array('code' => '00010001', 'name' => 'Can`t Match The End Tag'),                      //17
        'retry_crawl'       => array('code' => '00010010', 'name' => 'Retry Crawl Failed'),                         //18
        'cookie_error'    => array('code' => '00010011', 'name' => 'Cookie Is Expired - Need Update Cookie Data - Match No Login Tag'),                    //19
        'cookie_disabled'    => array('code' => '00010100', 'name' => 'Cookie Is Disabled - Need Update Cookie Data - Disabled Tag'),                  //20
        //重试代理相关
        ''          => array('code' => '00010101', 'name' => ''),                             //21
        //分析相关
        'merge_data'           => array('code' => '00001001', 'name' => 'Merge Data Result Is Null!'),                                      //9
        'match_split_data'           => array('code' => '00001010', 'name' => 'Match Or Split Failed'),                                     //10
        ''     => array('code' => '00010110', 'name' => ''),                            //22
        ''      => array('code' => '00011110', 'name' => ''),                     //30
        //参数错误
        'no_must_parameter'           => array('code' => '00001011', 'name' => 'Can`t Find The Required Parameter'),                  //11
        ''       => array('code' => '00001100', 'name' => ''),  //12
        ''          => array('code' => '00001101', 'name' => ''),                        //13
        ''            => array('code' => '00011010', 'name' => ''),                 //26
        //http 请求错误
        ''    => array('code' => '00001110', 'name' => ''),                         //14
        ''        => array('code' => '00011001', 'name' => ''),                                    //25
        //控制器路由错误
        'no_platform'          => array('code' => '00011011', 'name' => 'Can`t Provide Services For Platform '),                                      //27
        'phpError'              => array('code' => '00011100', 'name' => ''),                                          //28
        //account
        ''   => array('code' => '00011101', 'name' => '')                           //29
    );

    private static $_msgDelimeter = ' ';

    const CODE_TYPE_OFFSET          = 31;   //codeType In Complete Offset
    const ERROR_TYPE_OFFSET         = 24;   //errorType In Complete Offset
    const SYSTEM_CODE_OFFSET        = 20;   //systemCode In Complete Offset
    const SUB_SYSTEM_CODE_OFFSET    = 16;   //sub_systemCode In Complete Offset
    const MODULE_CODE_OFFSET        = 10;   //moduleCode In Complete Offset

    public function __construct(){

    }
    /**
     * 得到错误信息数组
     * @param $moduleCode array
     * @param $errorType array
     * @param $errorCodeAndMessage array
     * @return array
     */
    public static function getError($moduleName, $errorTypeName, $specificErrorName, $information = NULL ){
        $originalSystemCode = self::$systemCode['crawl'];
        $originalSubSysStemCode = self::$subSystemCode['keyword'];
        $array = array();

        $array['error'] = implode( self::$_msgDelimeter, array(
            self::$codeType['error']['name'],
            $originalSystemCode['name'],
            $originalSubSysStemCode['name'],
            self::$modulesCode[$moduleName]['name'],
            self::$errorType[$errorTypeName]['name'],
            '--',
            self::$specificError[$specificErrorName]['name']
        ));

        $hasError       = self::makeCode( self::$codeType['error']['code'], self::CODE_TYPE_OFFSET );
        $errorTypeCode  = self::makeCode( self::$errorType[$errorTypeName]['code'], self::ERROR_TYPE_OFFSET);
        $systemCode     = self::makeCode( $originalSystemCode['code'], self::SYSTEM_CODE_OFFSET );
        $subSystemCode  = self::makeCode( $originalSubSysStemCode['code'], self::SUB_SYSTEM_CODE_OFFSET );
        $moduleCode     = self::makeCode( self::$modulesCode[$moduleName]['code'], self::MODULE_CODE_OFFSET );
        $errorCode      = bindec( self::$specificError[$specificErrorName]['code'] );
        $code = decbin($hasError + $errorTypeCode + $systemCode + $subSystemCode + $moduleCode + $errorCode);
        $array['error_code'] = base_convert($code ,2,16);

        $array['request'] =  $_SERVER['PHP_SELF'];

        if( !empty( $information ) ) {
            $array['error'] .= ' : '.$information;
        }
        return $array;
    }

    private static function makeCode($code,$position){
        $code = $code << $position;
        return $code;
    }
}